﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Mvc;

namespace WebChatClient.Controllers
{
    public class HomeController : Controller
    {
        IWebService service;
       // HttpClient client = new HttpClient();
        List<Chatter> ListChatter;
        //
        // GET: /Home/

        public HomeController()
        {

            service = new WebChatService("http://localhost:63046/");

            ListChatter = new List<Chatter>();
        }

        public ActionResult Index()
        {
            Chatter tempchatter = GetCurrentChatterModel();
            return View(tempchatter);
        }

        private Chatter GetCurrentChatterModel()
        {
            Chatter tempchatter = new Chatter();
            string CurrentUserNick = User.Identity.Name;


            // Get the chatter list from the server
            HttpResponseMessage response = service.Get("api/ChatterAPI");
                 // Blocking call!
            if (response.IsSuccessStatusCode)
            {
                // Parse the response body. Blocking!
                var products = response.Content.ReadAsAsync<IEnumerable<Chatter>>().Result;
                foreach (var p in products)
                {
                    ListChatter.Add(p);
                }
            }

            //Compare the name to get right model

            for (int i = 0; i < ListChatter.Count; i++)
            {
                if (ListChatter[i].Nick == CurrentUserNick)
                    tempchatter = ListChatter[i];
            }
            return tempchatter;
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Image()
        {
            Chatter tempchatter = GetCurrentChatterModel();
            byte[] byteArray = ResizeImage( tempchatter.Avatar, 150, 210);
            return new FileContentResult(byteArray, "image/jpeg");
        }

        private static byte[] ResizeImage(byte[] byteArray, int maxHeight, int maxWidth)
        {

            Stream Imgstream = new MemoryStream(byteArray);

            System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(Imgstream);
            int imageHeight = imageToBeResized.Height;
            int imageWidth = imageToBeResized.Width;

            imageHeight = (imageHeight * maxWidth) / imageWidth;
            imageWidth = maxWidth;

            if (imageHeight > maxHeight)
            {
                imageWidth = (imageWidth * maxHeight) / imageHeight;
                imageHeight = maxHeight;
            }

            Bitmap bitmap = new Bitmap(imageToBeResized, imageWidth, imageHeight);
            System.IO.MemoryStream stream = new MemoryStream();
            bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
            stream.Position = 0;
            byte[] image = new byte[stream.Length + 1];
            stream.Read(image, 0, image.Length);
            return image;
        }
    }
}
